package game.leon.war;

import game.leon.war.game.WarGame;
import game.leon.war.gate.WarGate;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;

import java.io.IOException;

/**
 * User: bianpeng
 * Date: 12-1-9
 *
 * 网关服务器用来管理客户端, 以及用来连接各台游戏服务器
 * 需要连接到相应的游戏服务器上
 */
public class GateMain {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(GateMain.class);
        StopWatch watch = new StopWatch();
        // load context
        watch.start();
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{
                "war-gate-context.xml"
        });
        watch.stop();
        logger.info(String.format("context loaded in %d ms", watch.getLastTaskTimeMillis()));
        // start node
        // player visible
        WarGame game = context.getBean(WarGame.class);
        try {
            watch.start();
            game.start();
            watch.stop();
            logger.info(String.format("game started in %d ms", watch.getLastTaskTimeMillis()));
        } catch (IOException e) {
            logger.error("game start failed", e);
        }
        // start gate
        // node visible
        WarGate gate = context.getBean(WarGate.class);
        try {
            watch.start();
            gate.start();
            watch.stop();
            logger.info(String.format("gate started in %d ms", watch.getLastTaskTimeMillis()));
        } catch (IOException e) {
            logger.error("gate start failed", e);
        }
        ((ConfigurableApplicationContext)context).registerShutdownHook();
    }
}
